home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / controlsdemo / treeviewform.vb < prev   
Encoding:
Text File  |  2002-03-20  |  4.7 KB  |  112 lines

  1. Public Class TreeViewForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
  26.     Private components As System.ComponentModel.IContainer
  27.  
  28.     'Required by the Windows Form Designer
  29.  
  30.     'NOTE: The following procedure is required by the Windows Form Designer
  31.     'It can be modified using the Windows Form Designer.  
  32.     'Do not modify it using the code editor.
  33.     Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
  34.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  35.         Me.TreeView1 = New System.Windows.Forms.TreeView()
  36.         Me.CheckBox1 = New System.Windows.Forms.CheckBox()
  37.         Me.SuspendLayout()
  38.         '
  39.         'TreeView1
  40.         '
  41.         Me.TreeView1.ImageIndex = -1
  42.         Me.TreeView1.LabelEdit = True
  43.         Me.TreeView1.Location = New System.Drawing.Point(24, 24)
  44.         Me.TreeView1.Name = "TreeView1"
  45.         Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("TreeNode", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("First Child", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("First Grandchild"), New System.Windows.Forms.TreeNode("Second Grandchild"), New System.Windows.Forms.TreeNode("Third Grandchild")}), New System.Windows.Forms.TreeNode("Second Child"), New System.Windows.Forms.TreeNode("Third Child")})})
  46.         Me.TreeView1.SelectedImageIndex = -1
  47.         Me.TreeView1.Size = New System.Drawing.Size(240, 256)
  48.         Me.TreeView1.TabIndex = 0
  49.         '
  50.         'CheckBox1
  51.         '
  52.         Me.CheckBox1.Appearance = System.Windows.Forms.Appearance.Button
  53.         Me.CheckBox1.CheckAlign = System.Drawing.ContentAlignment.TopLeft
  54.         Me.CheckBox1.Location = New System.Drawing.Point(280, 24)
  55.         Me.CheckBox1.Name = "CheckBox1"
  56.         Me.CheckBox1.Size = New System.Drawing.Size(104, 56)
  57.         Me.CheckBox1.TabIndex = 2
  58.         Me.CheckBox1.Text = "Display Children Count"
  59.         Me.CheckBox1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
  60.         '
  61.         'TreeViewForm
  62.         '
  63.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  64.         Me.ClientSize = New System.Drawing.Size(400, 293)
  65.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.CheckBox1, Me.TreeView1})
  66.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  67.         Me.Name = "TreeViewForm"
  68.         Me.Text = "TreeViewForm"
  69.         Me.ResumeLayout(False)
  70.  
  71.     End Sub
  72.  
  73. #End Region
  74.  
  75.     ' enable/disable showing of children count
  76.  
  77.     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  78.         DisplayChildrenCount(TreeView1.Nodes, CheckBox1.Checked)
  79.     End Sub
  80.  
  81.     ' display the number of children nodes in the caption
  82.     ' of each node
  83.  
  84.     Sub DisplayChildrenCount(ByVal nodes As TreeNodeCollection, ByVal display As Boolean)
  85.         ' append the number of children to the node's Text
  86.         ' (after deleting existing count, if any)
  87.  
  88.         Dim node As TreeNode
  89.         For Each node In nodes
  90.             ' Remove any child count at the end of the Text, if there.
  91.             node.Text = System.Text.RegularExpressions.Regex.Replace(node.Text, " \[.*\]$", "")
  92.             ' Add it again, if so requested.
  93.             If display Then
  94.                 node.Text += " [" & CStr(node.Nodes.Count) & "]"
  95.             End If
  96.             ' Recurse over child nodes.
  97.             DisplayChildrenCount(node.Nodes, display)
  98.         Next
  99.     End Sub
  100.  
  101.     ' Refuse to edit a node that has a children count on its Text.
  102.  
  103.     Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit
  104.         If System.Text.RegularExpressions.Regex.IsMatch(e.Node.Text, " \[.*\]$") Then
  105.             e.CancelEdit = True
  106.             MessageBox.Show("You can't edit while children count is displayed")
  107.         End If
  108.     End Sub
  109.  
  110.  
  111. End Class
  112.